package com.teamreinforce.utils;

import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * This util class is for time uses.
 * The date and time formats are defined 
 * by this class.
 * 
 * @author Tidiane
 */

public class TeamReinforceTimeUtils {

  private static Calendar cal = Calendar.getInstance();
  private static SimpleDateFormat sdf;
  
  private static final String DATE_FORMAT = "dd-MM-yyyy";
  private static final String TIME_FORMAT = "HH:mm:ss";
  private static final String DAY_FORMAT = "dd";
  private static final String MONTH_FORMAT = "MM";
  private static final String YEAR_FORMAT = "yyyy";
  
  /**
   *  The the current date in 
   *  the string format according to the 
   *  pattern : yyyy-MM-dd
   */
  public static String DATE = getDate();
  
  /**
   * Get the time string 
   * like HH:mm:ss
   */
  public static String TIME = getTime();
  
  public static int eYear = getYear();
  public static int MONTH = getMonth();
  public static int eDay = getDay();
  
  /*
   * Get date string
   */
  private static String getDate() {
    sdf = new SimpleDateFormat(DATE_FORMAT);
    return sdf.format(cal.getTime());
  }
  
  /**
   * Get time string
   * @return 
   */
  private static String getTime(){
      sdf = new SimpleDateFormat(TIME_FORMAT);
      return sdf.format(cal.getTime());
  }
  
  /**
   * Get the the year
   * @return 
   */
  private static int getYear(){
      sdf = new SimpleDateFormat (YEAR_FORMAT);
      return Integer.parseInt(sdf.format(cal.getTime()));
  }
  
  /**
   * Get the month
   * @return 
   */
  private static int getMonth(){
      sdf = new SimpleDateFormat (MONTH_FORMAT);
      return Integer.parseInt(sdf.format(cal.getTime()));
  }
  
  /**
   * Get the day
   * @return eDay
   */ 
  private static int getDay(){
      sdf = new SimpleDateFormat (DAY_FORMAT);
      return Integer.parseInt(sdf.format(cal.getTime()));
  }
  
  /**
   * Calculate the duration between 
   * two dates
   * @return 
   */
  public static int calculateDuration (String startDate, String endDate){
      int _duration = 0;      String [] start = startDate.split("-");
      String [] end   = endDate.split("-");
      
      int year = Integer.parseInt(start[2]);
      int month = Integer.parseInt(start[1]);
      int day = Integer.parseInt(start[0]);
      
      int _year = Integer.parseInt(end[2]);
      int _month = Integer.parseInt(end[1]);
      int _day = Integer.parseInt(end[0]);

      if ( _day > day ) 
          _duration = _day - day;
      else
          _duration = day - _day;
      
      int _diffmonth = 0;
      if ( _year == year){
          _diffmonth = _month - month;
      } 
      else{
          _diffmonth = 12 - month + _month;
      }
       
      _duration +=  30 * _diffmonth;
      
      return _duration;
  }
  
  public static void main (String args []){
      System.out.println(" Duraation : " + calculateDuration("05-01-2004", "31-12-2005"));
  }
}